#!/usr/bin/env python
import json


class WrapCursor():
    def __init__(self):
        pass

    def Ok(self, error_desc):
        sj = {}
        sj["Code"] = 0
        sj["desc"] = error_desc
        sj['data'] = None
        return json.dumps(sj)

    def Error(self, error_desc):
        sj = {}
        sj["Code"] = 1
        sj["desc"] = error_desc
        sj['data'] = None
        return json.dumps(sj)

    def WrapCur(self, cur):
        data = cur.fetchall()
        col_name = [tuple1[0] for tuple1 in cur.description]
        return self.Wrap(data, col_name)

    def Wrap(self, data_set, col_name):
        sj = {"Code": 1, "desc": "no data"}
        data = []
        sj['data'] = data
        for k in range(0, len(data_set)):
            sj["Code"] = 0
            sj["desc"] = "success"
            r = data_set[k]
            rd = {}
            if r != None and len(r) != 0:
                for i in col_name:
                    rd[i] = r[col_name.index(i)]
                data.append(rd)
        return json.dumps(sj)

    def Wrap2Dict(self, data_set, col_name):
        sj = {}
        sj["Code"] = 1
        sj["desc"] = "no data"
        data = []
        sj['data'] = data
        for k in range(0, len(data_set)):
            sj["Code"] = 0
            sj["desc"] = "success"
            r = data_set[k]
            rd = {}
            if r != None and len(r) != 0:
                for i in col_name:
                    rd[i] = r[col_name.index(i)]
                data.append(rd)
        return (sj)

    @staticmethod
    def Cur2Dict(cur_o):
        data_set = cur_o.fetchall()
        col_name = [tuple1[0] for tuple1 in cur_o.description]
        sj = dict()
        sj["Code"] = 1
        sj["desc"] = "no data"
        data = []
        sj['data'] = data
        for k in range(0, len(data_set)):
            sj["Code"] = 0
            sj["desc"] = "success"
            r = data_set[k]
            rd = {}
            if r is not None and len(r) != 0:
                for i in col_name:
                    rd[i] = r[col_name.index(i)]
                data.append(rd)
        return sj
